const {
  getListService,
  addListService,
  updateListService,
  deleteListService,
  addGoodService,
  getGoodlistService,
  updategoodService,
  delgoodService,
} = require("../service/goods.service");
const fs = require("fs");

const fileService = require("../service/file.service");

class GoodsController {
  // 删除商品
  async delgood(ctx, next) {
    // console.log(ctx.request.params);
    const res = await delgoodService(ctx.request.params);
    console.log(res);
    if (res.length >= 1)
      return (ctx.body = {
        meta: {
          status: 200,
        },
        msg: "删除商品成功",
      });
    else {
      ctx.body = {
        meta: {
          status: 504,
        },
        msg: "删除失败",
      };
    }
  }
  // 修改商品信息
  async updategood(ctx, next) {
    console.log(ctx.request.body);
    const res = await updategoodService(ctx.request.body);
    // console.log(res);
    if (res.length >= 1)
      return (ctx.body = {
        meta: {
          status: 200,
        },
        msg: "修改商品成功",
      });
    else {
      ctx.body = {
        meta: {
          status: 504,
        },
        msg: "修改失败",
      };
    }
  }

  // 获取商品列表
  async getGoodlist(ctx, next) {
    // console.log(1234);
    console.log(ctx.request.query);
    const res = await getGoodlistService(ctx.request.query);
    // console.log(res);
    ctx.body = res;
  }

  // 获取0级管理列表
  async getList0(ctx, next) {
    const res = await getListService(0);
    ctx.body = res;
  }
  // 获取1级管理列表
  async getList1(ctx, next) {
    const res = await getListService(1);
    ctx.body = res;
  }
  // 添加分类
  async addList(ctx, next) {
    // console.log(ctx.request.body);
    try {
      const res = await addListService(ctx.request.body);
      // console.log(res);
      if (res.length >= 1) {
        ctx.body = {
          meta: {
            status: 200,
          },
          msg: "添加成功",
        };
      } else {
        ctx.body = {
          meta: {
            status: 0,
          },
          msg: "添加失败",
        };
      }
    } catch {
      ctx.body = {
        meta: {
          status: 0,
        },
        msg: "添加失败",
      };
    }
  }

  // 添加商品
  async addGood(ctx, next) {
    console.log(1);
    // console.log(ctx.request.body);
    const res = await addGoodService(ctx.request.body);
    // console.log(res);
    if (res.length >= 1)
      return (ctx.body = {
        meta: {
          status: 200,
        },
        msg: "增加商品成功",
      });
    else {
      ctx.body = {
        meta: {
          status: 504,
        },
        msg: "增加商品失败",
      };
    }
  }

  // 更新分类信息
  async updateList(ctx, next) {
    const res = await updateListService(ctx.request.body);
    // console.log(res);
    // ctx.body = res
    if (res.length >= 1) {
      ctx.body = {
        meta: {
          status: 200,
        },
        msg: "修改商品分类成功",
      };
    } else {
      ctx.body = {
        meta: {
          status: 504,
        },
        msg: "修改商品分类失败",
      };
    }
  }

  // 删除分类
  async deleteList(ctx, next) {
    // console.log(ctx.request.params.cat_id);
    const res = await deleteListService(ctx.request.params.cat_id);
    // console.log(res);
    if (res.length >= 1) {
      ctx.body = {
        meta: {
          status: 200,
        },
        msg: "删除成功",
      };
    } else {
      ctx.body = {
        meta: {
          status: 504,
        },
        msg: "删除失败",
      };
    }
  }

  // 查看用户头像
  async avatarInfo(ctx, next) {
    // 1.用户的头像是哪一个文件呢?
    const { goods_id } = ctx.params;
    const avatarInfo = await fileService.getAvatarByUserId(goods_id);
    console.log(avatarInfo);
    // 2.提供图像信息
    ctx.response.set("content-type", avatarInfo.mimetype);
    ctx.body = fs.createReadStream(`./upload/avator/${avatarInfo.filename}`);
  }
}
module.exports = new GoodsController();
